package de.dhbw.karlsruhe.picsim.model.commands.impl;

import de.dhbw.karlsruhe.picsim.model.actions.ActionFactory;
import de.dhbw.karlsruhe.picsim.model.actions.PicAction;
import de.dhbw.karlsruhe.picsim.model.memory.RegisterValue;
import de.dhbw.karlsruhe.picsim.model.memory.impl.RegisterValueImpl;
import de.dhbw.karlsruhe.picsim.util.Bits;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: goesen
 * Date: May 3, 2010
 * Time: 1:07:36 PM
 */
public class DecFSZ extends InstructionSupport {
    private static final Log log = LogFactory.getLog(DecFSZ.class);

    public DecFSZ(final Bits argument, final boolean targetIsWRegister) {
        super(targetIsWRegister, false, argument);
    }

    @NotNull
    @Override
    protected RegisterValue getValue(@NotNull final RegisterValue addressedFile, @NotNull final RegisterValue wRegister,
                                     @NotNull final List<PicAction> yourActions,
                                     @NotNull final ActionFactory actionFactory) {
        log.debug("DecFSZ gets executed");
        final int addressedValue = addressedFile.asInteger();
        final int result = addressedValue - 1;
        if (result == 0) {
            yourActions.add(actionFactory.createSkipNextCommandAction());
        }

        log.debug("result is " + result);
        return RegisterValueImpl.createUnsafe(result);
    }
}
